package com.example.swipytabs;

import java.util.Locale;
import java.util.Random;

import com.example.swipytabs.EditNameDialog.EditNameDialogListener;
import com.example.swipytabs.R.color;


import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.text.Editable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class TabCheckTextToSpeechFragment extends Fragment implements OnInitListener {
    public TabCheckTextToSpeechFragment() {
    }

    public static final String ARG_SECTION_NUMBER = "section_number";

     
    TextView textView1, textView2,textView3, textView4,textViewTest, textViewResults;
    Button tab2ButtonUSEnglish, tab2ButtonCandianEnglish, tab2ButtonFrench, tab2ButtonChinese;
    Button buttonNum0, buttonNum1, buttonNum2,buttonNum3,buttonEnter, buttonNext;
    Button buttonPlay, buttonClear;
    EditText editTextNumberEntered;
    View view2;
    TextToSpeech talker;
    String textToSpeek = "Default";
    Integer intTextToSpeek = 0;
    Locale languageLocale = Locale.US;  //default locale
    Random randomNumber = new Random();
    Integer numRight = 0, numWrong = 0;
    RadioGroup radioGroupNumberRange;
    Integer numberRange = 20;
    int numberOfPossibleCharacters = 0, numberOfButtons = 4;//the number of characters in the test set and # of buttons
	String[] chineseCharacters, pinyinArray, english ;
	Integer answerButton, characterChoosen; 
	randNonENumbs test1;  //array that holds the non repeating random number set


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	View v = inflater.inflate(R.layout.tabchecktexttospeech, null);
    	textView1 = (TextView)v.findViewById(R.id.textView1);
    	textView2 = (TextView)v.findViewById(R.id.textView2);
    	textView3 = (TextView)v.findViewById(R.id.textView3);
    	textView4 = (TextView)v.findViewById(R.id.textView4);
    	textViewTest = (TextView)v.findViewById(R.id.textViewTest);
    	textViewResults = (TextView)v.findViewById(R.id.textViewResults);
    	
  	    	

    	
    	tab2ButtonUSEnglish = (Button) v.findViewById(R.id.buttonTab2);
    	tab2ButtonUSEnglish.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onTab2ButtonUSEnglishClick(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});
    	
    	tab2ButtonCandianEnglish = (Button) v.findViewById(R.id.buttonTab2CheckCanadianEnglish);
    	tab2ButtonCandianEnglish.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onTab2ButtonCandianEnglishClick(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});
    	
    	tab2ButtonFrench = (Button) v.findViewById(R.id.buttonTab2CheckFrench);
    	tab2ButtonFrench.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onTab2ButtonFrenchClick(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});
    	
    	tab2ButtonChinese = (Button) v.findViewById(R.id.buttonTab2CheckChinese);
    	tab2ButtonChinese.setOnClickListener(new View.OnClickListener(){
    		public void onClick(View v){
    			//perform on on click
    			onTab2ButtonChineseClick(v);
    			//textView1.setText("I'm Clicked in onClick2");
    		}
    	});
  	    	
 	
    	
        return v;
    }
    

	public void say(String text2say){
    	talker.speak(text2say, TextToSpeech.QUEUE_FLUSH, null);
    }
    
    
    public void onTab2ButtonUSEnglishClick(View v){
    	//member to handle the button click
    	Activity activity = getActivity();

    	
    	talker = new TextToSpeech(getActivity(), this);

    	//textView2.setText("getDefaultEngine(): "+talker.getDefaultEngine());   //display the default engine
		//int results = talker.isLanguageAvailable(Locale.US);
		//textView4.setText(checkLanguageAvailability(results));
    	
    	//say("三十九");
    	languageLocale = Locale.US;
    	textToSpeek = "United States English is supported";
    	textView3.setText(textToSpeek);
    	Toast.makeText(activity, "Tab2 US English Button Clicked", Toast.LENGTH_SHORT ).show(); 
    }
    
    public void onTab2ButtonFrenchClick(View v){
    	//member to handle the button click
    	Activity activity = getActivity();

    	
    	talker = new TextToSpeech(getActivity(), this);

    	//textView2.setText("getDefaultEngine(): "+talker.getDefaultEngine());   //display the default engine
		//int results = talker.isLanguageAvailable(Locale.US);
		//textView4.setText(checkLanguageAvailability(results));
    	
    	//say("三十九");
    	languageLocale = Locale.FRENCH;
    	textToSpeek = "Je peux parler français";
    	textView3.setText(textToSpeek);
    	Toast.makeText(activity, "Tab2 French Button Clicked", Toast.LENGTH_SHORT ).show(); 
    }
    
    public void onTab2ButtonChineseClick(View v){
    	//member to handle the button click
    	Activity activity = getActivity();

    	
    	talker = new TextToSpeech(getActivity(), this);

    	//textView2.setText("getDefaultEngine(): "+talker.getDefaultEngine());   //display the default engine
		//int results = talker.isLanguageAvailable(Locale.US);
		//textView4.setText(checkLanguageAvailability(results));
    	
    	//say("三十九");
    	languageLocale = Locale.CHINESE;
    	//textToSpeek = "I can speek Chinese";
    	textToSpeek = "我说一点点中文 1234";
    	//textToSpeek = "1234";

    	textView3.setText(textToSpeek);
    	//Toast.makeText(activity, "Tab2 Chinese Button Clicked", Toast.LENGTH_SHORT ).show(); 
    	//showEditDialog(textToSpeek);
    	
    }
    
    public void onTab2ButtonCandianEnglishClick(View v){
    	//member to handle the button click
    	Activity activity = getActivity();
    	
    	talker = new TextToSpeech(getActivity(), this);

    	//textView2.setText("getDefaultEngine(): "+talker.getDefaultEngine());   //display the default engine
    	//textView4.setText(talker.toString());
    	
    	//say("三十九");
    	textToSpeek = "Canadian English is supported";
    	languageLocale = Locale.CANADA;
    	textView3.setText(textToSpeek);
    	Toast.makeText(activity, "Tab2 Candian English Button Clicked", Toast.LENGTH_SHORT ).show(); 
    }


	@Override
	public void onInit(int status) {
		// TODO Auto-generated method stub
				
 /*       //check for successful instantiation
		if (status == TextToSpeech.SUCCESS) {
			if(talker.isLanguageAvailable(Locale.US)==TextToSpeech.LANG_AVAILABLE)
				talker.setLanguage(Locale.US);
				talker.speak(textToSpeek, TextToSpeech.QUEUE_FLUSH, null);
				//int results = talker.isLanguageAvailable(Locale.US);
				//textView4.setText(checkLanguageAvailability(results));
			}
		else if (status == TextToSpeech.ERROR) {
			Toast.makeText(getActivity(), "Sorry! Text To Speech failed...", Toast.LENGTH_LONG).show();
			} */

		//check for successful instantiation
		if (status == TextToSpeech.SUCCESS) {
			int results = talker.isLanguageAvailable(languageLocale);
			if(results == TextToSpeech.LANG_AVAILABLE)
				talker.setLanguage(languageLocale);
				textView4.setText(checkLanguageAvailability(results));
				talker.speak(textToSpeek, TextToSpeech.QUEUE_FLUSH, null);
				textView2.setText("getDefaultEngine(): "+talker.getDefaultEngine());   //display the default engine
				
			}
		else if (status == TextToSpeech.ERROR) {
			Toast.makeText(getActivity(), "Sorry! Text To Speech failed...", Toast.LENGTH_SHORT).show();
			}		
		Toast.makeText(getActivity(), "I'm at init", Toast.LENGTH_SHORT).show();

	}
	


	@Override
	public void onDestroy() {
		if (talker != null) {
			talker.stop();
			talker.shutdown();
		}
		super.onDestroy();
	}
	
	public String checkLanguageAvailability(int results){
		String text = "text";
		
		switch (results){
			case TextToSpeech.LANG_NOT_SUPPORTED:
				text = Integer.toString(results)+": Language Not Supported";
				break;
			case TextToSpeech.LANG_AVAILABLE:
				text = "Language Available";
				break;
			case TextToSpeech.LANG_COUNTRY_AVAILABLE:
				text = "Language Country Available";
				break;				
			default:
				text = Integer.toString(results);
 	}
		
		return text;
		
	}

	
}
